package com.app.service.Impl;

import com.app.mapper.AdminUserMapper;
import com.app.mapper.UserMapper;
import com.app.pojo.Complaint;
import com.app.mapper.ComplaintMapper;
import com.app.pojo.User;
import com.app.service.IComplaintService;
import com.app.util.Result;
import com.app.vo.ComplaintVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 投诉建议 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2024-07-29
 */
@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint> implements IComplaintService {

    @Autowired
    private ComplaintMapper complaintMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public ComplaintVo getMyComplaintList(Integer page, Integer limit, Integer userid) {
        QueryWrapper<Complaint> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userid);
        queryWrapper.orderByDesc("create_time");
        Page<Complaint> page1 = new Page<>(page, limit);
        Page<Complaint> pageList = complaintMapper.selectPage(page1, queryWrapper);
        List<Complaint> list = pageList.getRecords();

        ComplaintVo vo = new ComplaintVo();
        vo.setCount((int) pageList.getTotal());
        vo.setList(list);
        return vo;
    }


    @Override
    public void addComplaint(Complaint complaint, Integer userid) {
        complaint.setUserId(userid);
        complaint.setStatus(1);
        complaint.setCreateTime(LocalDateTime.now());
        complaintMapper.insert(complaint);
    }

    /**
     * admin投诉意见列表查询
     *
     * @param page
     * @param limit
     * @param keyword
     * @return
     */
    @Override
    public Result<Object> complaintList(Integer page, Integer limit, String keyword) {
        Page p = new Page(page,limit);
        IPage<Complaint> complaintList = complaintMapper.complaintList(p,keyword);
        List<Complaint> list = complaintList.getRecords();
        for (Complaint complaint : list) {
            Integer userid = complaint.getUserId();
            System.err.println("userid:" + userid);
            User user = userMapper.selectById(userid);
            System.err.println("user:" + user);
            String username = user.getName();
            String phone = user.getMobile();
            complaint.setUserPhone(phone);
            complaint.setUserName(username);
        }
        ComplaintVo vo = new ComplaintVo();
        vo.setCount((int) complaintList.getTotal());
        vo.setList(complaintList.getRecords());
        return Result.success(vo);
    }

    /**
     * admin删除投诉意见
     * @param id
     * @return
     */
    @Override
    public Result<Object> deleteById(Integer id) {
        Complaint complaint = new Complaint();
        complaint.setId(id);
        complaint.setStatus(0);
        complaintMapper.updateById(complaint);
        return Result.success("删除成功");
    }
}
